package com.tarena.rabbitmq.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class RabbitDemoConfiguration {
    //首先声明的是 业务交换机 direct类型
    @Bean
    public DirectExchange businessExchange(){
        log.info("声明业务交换机");
        return new DirectExchange("business-exchange");
    }
    //死信交换机 direct类型
    @Bean
    public DirectExchange deadLetterExchange(){
        log.info("声明死信交换机");
        return new DirectExchange("dead-letter-exchange");
    }
    //死信队列
    @Bean
    public Queue deadLetterQueue(){
        log.info("声明死信队列");
        return QueueBuilder.nonDurable("dead-letter-queue").build();
    }
    //声明业务队列 指定x-dead-letter-exchange到死信交换机 ttl10秒 死信队列
    @Bean
    public Queue businessQueue(){
        log.info("声明业务队列");
        return QueueBuilder.nonDurable("business-queue").deadLetterExchange("dead-letter-exchange")
            .deadLetterRoutingKey("dead-letter-routing-key").ttl(10000).build();
    }
    //业务绑定
    @Bean
    public Binding businessBind(){
        log.info("业务绑定");
        //queueBind(String queue, String exchange, String routingKey)
        return BindingBuilder.bind(businessQueue()).to(businessExchange()).with("business");
    }
    @Bean
    public Binding deadLetterBind(){
        log.info("死信绑定");
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dead-letter-routing-key");
    }
}
